#ifndef TCPSERVER_H
#define TCPSERVER_H

#include <QtNetwork>
#include <QObject>
#include <functional>
#include <QThread>
#include <mutex>

#include "tcpsession.h"

class TcpServer : public QTcpServer
{
    Q_OBJECT
public:
    TcpServer();
    ~TcpServer();

    bool Start(unsigned short port);
    void Stop();

    size_t GetSessionSize() const;
    bool IsStart();

    std::function<void(std::shared_ptr<TcpSession> &)> onAccepted = nullptr;

protected:
    virtual void incomingConnection(qintptr handle);

private slots:
    void SlotDisconnect(const QString&);

private:
    bool _IsRun = false;
    QThread* _thread;

    std::mutex _mutex;
    std::map<std::string, std::shared_ptr<TcpSession>> m_SessionVector;
};

#endif // TCPSERVER_H
